package com.athuahua.mq.listener;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @Author: huahua
 * @Date: 2024/9/3 0003
 * @Time: 13:29
 * @Description:
 */
@Component
@Slf4j
public class MyMessageListener {
    public static final String EXCHANGE_NAME = "exchange.direct.order";
    public static final String ROUTING_KEY = "order";
    public static final String QUEUE_NAME = "queue.order";

    public static final String QUEUE_DELAY = "queue.test.delay";

    /**
     * 监听 + 创建交换机和队列
     *
     * @RabbitListener(bindings = @QueueBinding(
     * value = @Queue(value = QUEUE_NAME, durable = "true"),  //队列名 持久化
     * exchange = @Exchange(value = EXCHANGE_NAME),  //交换机名 类型为direct
     * key = ROUTING_KEY) //路由键
     * )
     */
    //只监听队列
//    @RabbitListener(queues = QUEUE_NAME)
//    public void processMessage(String dataString, Message message, Channel channel) throws IOException {
//
//
//        //获取消息的交付标签
//        long deliveryTag = message.getMessageProperties().getDeliveryTag();
//        try {
//            // 核心操作
//            System.out.println("消费端接收到消息: " + dataString);
//            //模拟业务异常 System.out.println(10 / 0);
//
//
//            //核心操作成功 返回 ACK 信息
//            //basiAck(交付标签”每一个消息的唯一标签“, 是否批量确认”一般为false“)
//            channel.basicAck(deliveryTag, false);
//
//        } catch (Exception e) {
//            //核心操作失败 返回 NACK信息
//            //获取消息是否是重新投递的
//            boolean redelivered = message.getMessageProperties().getRedelivered();
//            if (redelivered) {
//                //重新投递次数过多，放弃处理
//                channel.basicNack(deliveryTag, false, false);
//                log.error("消息处理失败，重新投递次数过多，放弃处理！");
//            } else {
//                //处理失败，重新入队
//                //basicNack(交付标签, 是否批量确认, 是否重新投递 )
//                channel.basicNack(deliveryTag, false, true);
//            }
//
//
//            throw new RuntimeException(e);
//        }
//
//
//    }
    @RabbitListener(queues = QUEUE_NAME)
    public void processMessageTestPrefetch(String dataString, Message message, Channel channel) throws IOException, InterruptedException {
        System.out.println("消费端接收到消息: " + dataString);

        TimeUnit.SECONDS.sleep(1);

        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        channel.basicAck(deliveryTag, false);
    }

    @RabbitListener(queues = QUEUE_DELAY)
    public void processMessageDelay(String dataString, Message message, Channel channel) throws IOException, InterruptedException {
        log.info("消费端接收到延迟消息: " + dataString);
        log.info("当前时间: {}", new SimpleDateFormat("HH:mm:ss").format(new Date()));


        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
